// pages/Preferred/index.js

var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        windowHeight: 100,
        hiddenLoading: false,
        currentItem: '',
        catList1: [],
        catList2: [],
        tabber: {},
        active: 0,
        goodsList: [],
        perPage: 20,
        curPage: 1,
        class_id: 103,
        is_vip_price: JSON.parse(wx.getStorageSync('is_vip_price') || 'false'),
        userInfo: {}
    },
    onShow() {
        var that = this

        this.setData({
            is_shop: wx.getStorageSync('is_shop') || 0,
        })

        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo,
                is_vip_price: userInfo.is_vip_price
            });
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.editTabbar();
        //初始化
        this.setCategoryData();
        //获取设备窗口信息
        var me = this;
        wx.getSystemInfo({
            success: function (res) {
                me.setData({
                    windowHeight: res.windowHeight
                })
            }
        })
    },
    onChange(event) {

        this.setData({
            class_id: event.detail.name.id
        })
        this.setGoodsListData(event.detail.name.id)
    },
    //初始化分类
    setCategoryData: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['pid'] = 110;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_goods_category',
            data: {
                pid: paraArr['pid'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                that.setData({
                    catList1: res.data,
                    currentItem: res.data[0].id
                });
                that.setGoodsListData(res.data[0].id);
            }
        })
    },

    //初始化子类
    setChildCategoryData: function (id) {
        var that = this;
        var paraArr = new Array();
        paraArr['pid'] = id;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api_goods_category',
            data: {
                pid: paraArr['pid'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                that.setData({
                    catList2: res.data
                });
            }
        })
        that.setData({
            currentItem: id,
            hiddenLoading: true
        });
    },

    //切换大类
    toggleCategory: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id
        if (!id) return;
        that.setChildCategoryData(id);
    },

    //点击小类
    goGoodsList: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id
        if (!id) return;
        console.log('goods/list', 'id=' + id);
        app.redirect('goods/list', 'id=' + id)
    },
    //初始商品列表
    setGoodsListData: function (id) {
        var that = this;
        wx.request({
            url: rootDocment + '/api_goods',
            data: {
                sort: id,
                size: that.data.perPage,
                page: that.data.curPage,
                user_id: wx.getStorageSync('user_id3') || 0,
                order: 'id',
                order_type: 'desc',
            },
            method: 'GET',
            header: {},
            success: function (res) {

                if (res.statusCode == 200) {
                    console.log(res.data.data);
                    that.setData({
                        goodsList: res.data.data
                    });
                }

                that.setData({
                    hiddenLoading: true
                });
                // if (res.data.data.length > 0) { //如果有数据
                //   var list = that.data.goodsList;
                //   for (var i = 0; i < res.data.data.length; i++) {
                //     list.push(res.data.data[i]);
                //   }
                //   that.setData({
                //     goodsList: list
                //   });
                //   if (res.data.data.length == that.data.perPage) {
                //     that.setData({
                //       curPage: that.data.curPage + 1,
                //       hiddenLoading: true
                //     });
                //   } else {
                //     that.setData({
                //       hiddenLoading: true
                //     });
                //   }
                // } else {
                //   that.setData({
                //     hiddenLoading: true

                //   });
                // }

            }
        })

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})